﻿using ModernShopping.AppCode;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ModernShopping.User
{
    public partial class ProductInStore : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            LoginInfo loginInfo = Session.GetLoginInfo();
            if (loginInfo == null)
            {
                Response.GoToAccessDeniedPage(Request);
            }
            if (!loginInfo.IsUser())
            {
                Response.GoToAccessDeniedPage(Request);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DbStore.InitDropdownList(cboListStore, Session.GetLoginInfo().UserID);
                if (cboListStore.Items.Count > 0)
                {
                    int code = int.Parse(cboListStore.SelectedValue);
                    lblAddress.Text = DbStore.GetByID(code).Address;
                    lblProductCount.Text = string.Format("Cửa hàng này hiện có {0} sản phẩm", gvMyStores.Rows.Count);
                    DbProductStore.initGridviewByStore(gvMyStores, code);
                }
            }
        }

        protected void btnShow_Click(object sender, EventArgs e)
        {
            int code = int.Parse(cboListStore.SelectedValue);
            DbProductStore.initGridviewByStore(gvMyStores, code);
            lblProductCount.Text = string.Format("Cửa hàng này hiện có {0} sản phẩm", gvMyStores.Rows.Count);
        }

        protected void cboListStore_SelectedIndexChanged(object sender, EventArgs e)
        {
            int code = int.Parse(cboListStore.SelectedValue);
            lblAddress.Text = DbStore.GetByID(code).Address;
        }

        protected void gvMyStores_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvMyStores.PageIndex = e.NewPageIndex;
            int code = int.Parse(cboListStore.SelectedValue);
            DbProductStore.initGridviewByStore(gvMyStores, code);
        }

        private void RefreshGridview()
        {
            int code = int.Parse(cboListStore.SelectedValue);
            DbProductStore.initGridviewByStore(gvMyStores, code);
        }

        public delegate void DelRefreshGridview();

        protected void lbRemove_Click(object sender, EventArgs e)
        {
            Countdown.ShowPopup();
            LinkButton lb = sender as LinkButton;
            string[] Arguments = lb.CommandArgument.Split(',');
            int ProductCode = int.Parse(Arguments[0]);
            int CodeStore = int.Parse(Arguments[1]);

            if (DbProductStore.Delete(ProductCode, CodeStore))
            {
                Countdown.InitCountdown(1, "Đã xóa ra khỏi cửa hàng", WebUtil.Page.PRODUCT_IN_STORE);
                Countdown.Start();
            }
            else
            {
                Countdown.Stop();
            }
        }
    }
}